<template>
  <el-dialog
    :visible.sync="dialogVisible"
    width="800px"
    :before-close="handleClose"
    :close-on-click-modal="false"
    append-to-body
    :title="title"
  >
    <el-form :model="form" ref="formRef" label-width="90px">
      <el-form-item
        label="用户名"
        prop="nick_name"
        :rules="[{ required: true, message: '请输入用户名', trigger: 'blur' }]"
      >
        <el-input placeholder="请输入" v-model="form.nick_name"></el-input>
      </el-form-item>

      <el-form-item
        label="性别"
        prop="gender"
        :rules="[{ required: true, message: '请输入性别', trigger: 'blur' }]"
      >
        <el-select v-model="form.gender" placeholder="请选择性别" clearable>
          <el-option
            v-for="item in optionsData"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          ></el-option>
        </el-select>
        <!--    <el-input placeholder="请输入" v-model="form.gender"></el-input> -->
      </el-form-item>

      <!-- <el-form-item label="登录密码" prop="password" :rules="[{ required: true, message: '请输入登录密码', trigger: 'blur' }]">
        <el-input
          placeholder="请输入"
          v-model="form.password"
        ></el-input>
      </el-form-item> -->

      <el-form-item label="头像" prop="icon">
        <!--  {{ imgsDefalut }} -->
        <ImgUploadPath
          ref="ImgUploadPath"
          :limit="1"
          @success="onUploadSuccess"
          @remove="onUploadRemove"
          :imgsDefalut="imgsDefalut"
        />
      </el-form-item>

      <el-form-item
        label="手机号"
        prop="phone"
        :rules="[{ required: true, validator: validatePhone, trigger: 'blur' }]"
      >
        <el-input placeholder="请输入" v-model="form.phone"></el-input>
      </el-form-item>

      <!--   <el-form-item
        label="专业擅长"
        prop="introduce"
        :rules="[
          { required: true, message: '请输入专业擅长', trigger: 'blur' },
        ]"
      >
        <el-input
          resize="none"
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4 }"
          placeholder="请输入"
          v-model="form.introduce"
        ></el-input>
      </el-form-item>
 -->
      <!--  <el-form-item
        label="职称"
        prop="position"
        :rules="[{ required: true, message: '请输入职称', trigger: 'blur' }]"
      >
        <el-input placeholder="请输入" v-model="form.position"></el-input>
      </el-form-item>
 -->
      <!--   <el-form-item
        label="所属科室"
        prop="department_id"
        :rules="[
          { required: true, message: '请选择所属科室', trigger: 'change' },
        ]"
      >
        <el-select
          v-model="form.department_id"
          clearable
          filterable
          placeholder="请选择"
        >
          <el-option
            v-for="item in departmentData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
 -->
      <!--    <el-form-item
        label="所属角色"
        prop="role_id"
        :rules="[
          { required: true, message: '请选择所属角色', trigger: 'change' },
        ]"
      >
        <el-select
          v-model="form.role_id"
          clearable
          filterable
          placeholder="请选择"
        >
          <el-option
            v-for="item in roleData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item> -->
      <!--  <el-form-item
        label="所属机构"
        prop="unit"
        :rules="[
          { required: true, message: '请输入所属机构', trigger: 'blur' },
        ]"
      >
        <el-input placeholder="请输入" v-model="form.unit"></el-input>
      </el-form-item> -->
    </el-form>
    <div slot="footer" class="tx-c">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="confirm" :loading="btnLoading"
        >确认</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { debounce } from "@/util";
import { ApiUserEdit } from "@/api/index.js";
import ImgUploadPath from "@/components/imgUpload-path.vue";
import { validatePhone } from "@/util/validate";
export default {
  props: ["roleData", "departmentData"],
  components: {
    ImgUploadPath,
  },
  data() {
    return {
      validatePhone,
      optionsData: [
        { name: "男", code: "男" },
        { name: "女", code: "女" },
      ],
      oldform: {
        nick_name: "",
        phone: "",
        gender: "",
        icon: "",
        /*    unit: "京山市人民医院", */
      },
      form: {},
      btnLoading: false,
      dialogVisible: false,
      id: null,
      title: "",
      imgsDefalut: [],
    };
  },

  methods: {
    onUploadSuccess(path) {
      console.log("111", path);
      this.form.icon = path;
    },
    onUploadRemove() {
      this.form.icon = "";
    },
    confirm: debounce(
      function () {
        this.$refs["formRef"].validate(async (valid) => {
          if (valid) {
            this.btnLoading = true;

            try {
              if (this.id) {
                await ApiUserEdit(this.form, this.id);
                this.$message.success("修改成功");
              } else {
                const form = { ...this.form, password: "123456" };
                await ApidoctorAdd(form);
                this.$alert("添加成功，该员工初始密码为：123456", "提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {},
                });
              }

              this.handleClose();
              this.$emit("refresh");
            } finally {
              this.btnLoading = false;
            }
          } else return false;
        });
      },
      500,
      true
    ),

    show(row) {
      console.log(row);
      const data = row || this.oldform;
      for (const key in this.oldform) {
        this.$set(this.form, key, data[key]);
      }
      if (row) {
        this.id = row.id;
        this.title = "编辑人员";
        if (row.icon) this.imgsDefalut = [{ url: this.$path + row.icon }];
      } else {
        this.id = null;
        this.title = "添加人员";
      }
      this.dialogVisible = true;
    },

    handleClose() {
      this.dialogVisible = false;
      this.form = { ...this.oldform };
      this.$nextTick((_) => {
        this.$refs.formRef.clearValidate();
        this.$refs.ImgUploadPath.clear();
      });
    },
  },
};
</script>

<style></style>
